package com.ewanghuiju.app.ui.my.activity;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RootActivity;
import com.ewanghuiju.app.base.contract.my.VipUpdataOrderRecordContract;
import com.ewanghuiju.app.model.bean.response.VipUpOrderRecordResponBean;
import com.ewanghuiju.app.presenter.my.VipUpdataOrderRecordPresenter;
import com.ewanghuiju.app.test.bean.TestBean;
import com.ewanghuiju.app.ui.my.adapter.VipUpdataOrderRecordsAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 会员升级订单记录页面
 * @time 2020/2/19
 */
public class VipUpdataOrderRecordActivity extends RootActivity<VipUpdataOrderRecordPresenter> implements VipUpdataOrderRecordContract.View {
    @BindView(R.id.view_main)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private VipUpdataOrderRecordsAdapter vipUpdataOrderRecordsAdapter;


    private int currentPage = 1;

    @Override
    protected int getLayout() {
        return R.layout.activity_vip_updata_order_record;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        initRecyclerView();
    }

    @Override
    protected int getTitleIntText() {
        return R.string.order_record_title;
    }

    @Override
    protected void intSmartRefreshLayout() {
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {


            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getData();
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData();
            }
        });

        //首次进入显示
        LoadingDialogUtils.show(mContext);
        currentPage = 1;
        mPresenter.getData();
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        vipUpdataOrderRecordsAdapter = new VipUpdataOrderRecordsAdapter(R.layout.item_vip_uporder_record);
        recyclerView.setAdapter(vipUpdataOrderRecordsAdapter);
        vipUpdataOrderRecordsAdapter.setNewData(TestBean.getVipUpOrderRecordResponBeanList(5));
    }

//    @OnClick({R.id.iv_back})
//    public void doClick(View view) {
//        if (AntiShake.check(view.getId())) {    //判断是否多次点击
//            return;
//        }
//        switch (view.getId()) {
//            case R.id.iv_back:
//                finish();
//                break;
//            default:
//                break;
//        }
//    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {

    }

    @Override
    public void showContent(List<VipUpOrderRecordResponBean> list) {
        //获取记录成功
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            vipUpdataOrderRecordsAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            vipUpdataOrderRecordsAdapter.addData(list);
        }

        if (vipUpdataOrderRecordsAdapter.getData().size() > 0) {
            stateMain();
        } else {
            stateEmpty();
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (vipUpdataOrderRecordsAdapter.getData().size() == 0) {
            super.stateError();
        }
    }
}
